package cn.edu.fzu.homemaking.controller;

import cn.edu.fzu.homemaking.api.SsoApi;
import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.domain.entity.User;
import cn.edu.fzu.homemaking.domain.entity.UserAddress;
import cn.edu.fzu.homemaking.domain.vo.UserAddressVo;
import cn.edu.fzu.homemaking.service.AddressService;
import cn.edu.fzu.homemaking.web.Result;
import cn.edu.fzu.homemaking.web.RetCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author jiebin
 * @Date 2023/4/4
 */
@RestController
@RequestMapping("/api/user/auth/address")
@Api(tags = "用户地址管理")
public class AddressController {

    @Resource
    private AddressService addressService;

    @Resource
    private SsoApi ssoApi;

    @PostMapping("insert")
    @ApiOperation(value = "添加地址信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名",required = true),
            @ApiImplicitParam(name = "areaId", value = "地区id",required = true),
            @ApiImplicitParam(name = "address", value = "详细地址",required = true),
            @ApiImplicitParam(name = "phone", value = "联系电话", required = true),
            @ApiImplicitParam(name = "email", value = "邮箱地址", required = true)
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result insert(String name, Long areaId, String address, String phone, String email,
                         HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        addressService.insert(user.getId(), name, areaId, address, phone, email);
        return new Result(RetCode.SUCCESS, "添加地址成功");
    }

    @PostMapping("deleteById")
    @ApiOperation(value = "删除地址信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址id", required = true)
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result deleteByid(Long id, HttpServletRequest request){
        User user = (User)request.getSession().getAttribute(SessionConstant.ACCOUNT);
        addressService.deleteById(id, user);
        return new Result(RetCode.SUCCESS, "删除地址成功");
    }

    @PostMapping("updateById")
    @ApiOperation(value = "更新地址信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址id", required = true),
            @ApiImplicitParam(name = "name", value = "用户名"),
            @ApiImplicitParam(name = "areaId", value = "地区id"),
            @ApiImplicitParam(name = "address", value = "用户详细地址"),
            @ApiImplicitParam(name = "phone", value = "联系电话"),
            @ApiImplicitParam(name = "email", value = "邮箱地址")
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result updateById(Long id, String name, Long areaId, String address,
                             String phone, String email, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        addressService.updateById(id, name, areaId, address, phone, email,user);
        return new Result(RetCode.SUCCESS, "更新地址成功");
    }

    @GetMapping("selectById")
    @ApiOperation(value = "根据id查询地址", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址id", required = true)
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result selectById(Long id){
        UserAddress userAddress = addressService.selectById(id);
        return  new Result(userAddress);
    }

    @GetMapping("list")
    @ApiOperation(value = "根据用户id获取地址列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true)
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(Integer pageNum, Integer pageSize, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        try(Page<UserAddress> page = PageHelper.startPage(pageNum, pageSize)){
            List<UserAddressVo> list = addressService.list(user.getId());
            return new Result(new PageVO<>(page.getTotal(),list));
        }
    }
}
